home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_11_12
/
splash
/
splash.h
< prev
next >
Wrap
Text File
|
1993-01-14
|
23KB
|
856 lines
/*
* Version 1.8
* Written by Jim Morris, jegm@sgi.com
* Kudos to Larry Wall for inventing Perl
* Copyrights only exist on the regex stuff, and all have been left intact.
* The only thing I ask is that you let me know of any nifty fixes or
* additions.
*
* Credits:
* I'd like to thank Michael Golan <mg@Princeton.EDU> for his critiques
* and clever suggestions. Some of which have actually been implemented
*/
#ifndef _SPLASH_H
#define _SPLASH_H
#include <string.h>
#include "regexp.h"
#if DEBUG
#include <stdio.h>
#endif
#define INLINE inline
//************************************************************
// This is the base class for SPList, it handles the underlying
// dynamic array mechanism
//************************************************************
template<class T>
class SPListBase
{
private:
enum{ALLOCINC=20};
T *a;
int cnt;
int first;
int allocated;
int allocinc;
void grow(int amnt= 0, int newcnt= -1);
protected:
void compact(const int i);
public:
#ifdef USLCOMPILER
// USL 3.0 bug with enums losing the value
SPListBase(int n= 20)
#else
SPListBase(int n= ALLOCINC)
#endif
{
a= new T[n];
cnt= 0;
first= n>>1;
allocated= n;
allocinc= n;
# ifdef DEBUG
fprintf(stderr, "SPListBase(int %d) a= %p\n", allocinc, a);
# endif
}
SPListBase(const SPListBase<T>& n);
SPListBase<T>& SPListBase<T>::operator=(const SPListBase<T>& n);
virtual ~SPListBase(){
# ifdef DEBUG
fprintf(stderr, "~SPListBase() a= %p, allocinc= %d\n", a, allocinc);
# endif
delete [] a;
}
INLINE T& operator[](const int i);
INLINE const T& operator[](const int i) const;
int count(void) const{ return cnt; }
void add(const T& n);
void add(const int i, const T& n);
void erase(void){ cnt= 0; first= (allocated>>1);}
};
// forward declarations
class SPStringList;
class Slice;
template <class T> class SPList;
template <class T> class SubList;
//************************************************************
// Slice class to keep track of, and create, slices
//************************************************************
#include <stdarg.h>
class Slice
{
private:
SPList<Range> *rl;
public:
inline Slice();
Slice(const char *); // parse the string to get a slice
Slice(int n, ...); // list of indices to add to slice
inline Slice(const Slice& slc);
inline Slice(const Range& r);
inline ~Slice();
inline int count(void) const;
inline const Range& operator[](int i) const;
void add(int i); // add one element to slice
friend ostream& operator<<(ostream&, const Slice&);
};
//************************************************************
// Allows assignment to slices of a list
//************************************************************
template <class T>
class SubList
{
private:
// This has to be a pointer because we don't know the size of Splice
// and there is a nasty cyclic interdependency between the next 3 classes
Slice *sl; // because we may want to use a temp in call T/O convenience with efficiency
SPList<T>& l;
public:
SubList(SPList<T>& lst, const Slice& slc);
SubList(SPList<T>& lst, int st, int len);
SubList(SPList<T>& lst, const Range& r);
~SubList();
SubList<T>& operator=(const SPList<T>& lst);
friend SPList<T>;
};
//************************************************************
// SPList
//************************************************************
template <class T>
class SPList: private SPListBase<T>
{
public:
SPList(int sz= 10): SPListBase<T>(sz){}
SPList(const SubList<T>& sbl);
// stuff I want public to see from SPListBase
T& operator[](const int i){return SPListBase<T>::operator[](i);}
const T& operator[](const int i) const{return SPListBase<T>::operator[](i);}
SPListBase<T>::count; // some compilers don't like this
// add perl-like synonyms
void reset(void){ erase(); }
int scalar(void) const { return count(); }
operator void*() { return count()?this:0; } // so it can be used in tests
int isempty(void) const{ return !count(); } // for those that don't like the above (hi michael)
T pop(void)
{
T tmp;
int n= count()-1;
if(n >= 0){
tmp= (*this)[n];
compact(n);
}
return tmp;
}
void push(const T& a){ add(a);}
void push(const SPList<T>& l);
T shift(void)
{
T tmp= (*this)[0];
compact(0);
return tmp;
}
int unshift(const T& a)
{
add(0, a);
return count();
}
int unshift(const SPList<T>& l);
SPList<T> reverse(void);
SPList<T> sort();
SPList<T> splice(int offset, int len, const SPList<T>& l);
SPList<T> splice(int offset, int len);
SPList<T> splice(int offset);
SubList<T> operator()(int st, int len){return SubList<T>(*this, st, len);}
SubList<T> operator()(const Range& r){return SubList<T>(*this, r);}
SubList<T> operator()(const Slice& slc){return SubList<T>(*this, slc);}
SubList<T> operator()(const char *s){return SubList<T>(*this, Slice(s));}
};
//****************************************************************
// just a mechanism for self deleteing strings which can be hacked
//****************************************************************
class TempString
{
private:
char *str;
public:
TempString(const char *s)
{
str= new char[strlen(s) + 1];
strcpy(str, s);
}
TempString(const char *s, int len)
{
str= new char[len + 1];
if(len) strncpy(str, s, len);
str[len]= '\0';
}
~TempString(){ delete [] str; }
operator char*() const { return str; }
};
//************************************************************
// This class takes care of the mechanism behind variable
// length strings
//************************************************************
class VarString
{
private:
enum{ALLOCINC=32};
char *a;
int len;
int allocated;
int allocinc;
INLINE void grow(int n= 0);
public:
#ifdef USLCOMPILER
// USL 3.0 bug with enums losing the value
INLINE VarString(int n= 32);
#else
INLINE VarString(int n= ALLOCINC);
#endif
INLINE VarString(const VarString& n);
INLINE VarString(const char *);
INLINE VarString(const char* s, int n);
INLINE VarString(char);
~VarString(){
# ifdef DEBUG
fprintf(stderr, "~VarString() a= %p, allocinc= %d\n", a, allocinc);
# endif
delete [] a;
}
VarString& operator=(const VarString& n);
VarString& operator=(const char *);
INLINE const char operator[](const int i) const;
INLINE char& operator[](const int i);
operator const char *() const{ return a; }
int length(void) const{ return len; }
void add(char);
void add(const char *);
void add(int, const char *);
void remove(int, int= 1);
void erase(void){ len= 0; }
};
class SPStringList;
//************************************************************
// Implements the perl specific string functionality
//************************************************************
class SPString
{
private:
VarString pstr; // variable length string mechanism
public:
class substring;
SPString():pstr(){}
SPString(const SPString& n) : pstr(n.pstr){}
SPString(const char *s) : pstr(s){}
SPString(const char c) : pstr(c){}
SPString(const substring& sb) : pstr(sb.pt, sb.len){}
SPString& operator=(const char *s){pstr= s; return *this;}
SPString& operator=(const SPString& n);
SPString& operator=(const substring& sb);
operator const char*() const{return pstr;}
const char operator[](int n) const{ return pstr[n]; }
int length(void) const{ return pstr.length(); }
char chop(void);
int index(const SPString& s, int offset= 0);
int